local brain = require("brains/magicalgirl_hunterbrain")

local RETARGET_MUST_TAGS = { "_combat" }
local RETARGET_CANT_TAGS = { "hound", "walrus", }
local RETARGET_ONEOF_TAGS = { "animal", "character", "monster", "magicalgirl" }
local function Retarget(inst)
	return FindEntity(inst, TUNING.WALRUS_TARGET_DIST, function(guy)
			return not SuicideCharacters[guy.prefab] and inst.components.combat:CanTarget(guy)
		end,
		RETARGET_MUST_TAGS,
		RETARGET_CANT_TAGS,
		RETARGET_ONEOF_TAGS
	)
end

local function OnAttackOther(inst, target)
	if not target then
		return
	end

	if target.components.locomotor and math.random() < 0.15 then
		target.components.locomotor:SetExternalSpeedMultiplier(target, "magicalgirl_hunter", 0.5)
		if target.magicalgirl_hunter_task then --alt:add magicalgirl_hunter_task
			target.magicalgirl_hunter_task:Cancel()
		end
		target.magicalgirl_hunter_task = target:DoTaskInTime(5, function()
			target.components.locomotor:RemoveExternalSpeedMultiplier(target, "magicalgirl_hunter")
		end)
	end
	--[[
	if inst.havy_attack then
		inst.components.timer:StartTimer("havy_attack", 30)

		inst.havy_attack = false
		inst.components.planardamage:AddBonus(inst, 0, "havy_attack")
	end]]
end

local function OnTimerDone(inst, data)
	--[[if data and data.name == "havy_attack" then
		inst.havy_attack = true
		inst.components.planardamage:AddBonus(inst, 20, "havy_attack")
	end]]
end

local function fn()
	local inst = Prefabs["walrus"].fn()

	if not TheWorld.ismastersim then
		return inst
	end

	--[[inst:AddComponent("timer")
	inst.components.timer:StartTimer("havy_attack", 30)]]

	inst.components.combat:SetRetargetFunction(1, Retarget)
	local _DoAttack = inst.components.combat.DoAttack
	inst.components.combat.DoAttack = function(self, target, ...)
		_DoAttack(self, target, ...)
		OnAttackOther(self.inst, target)
	end

	inst:AddComponent("planardamage")
	inst.components.planardamage:AddBonus(inst, 20, "havy_attack")

	inst:SetBrain(brain)
	inst:ListenForEvent("timerdone", OnTimerDone)

	return inst
end

return Prefab("magicalgirl_hunter", fn)
